# Replication Archive for: Aggarwal, Minali, Jennifer Allen, Alex Coppock, 
# Dan Frankowski, Sol Messing, Kelly Zhang, James Barnes, Andrew Beasley, 
# Harry Hantman, and Sylvan Zheng: The impact of digital campaign advertising 
# during the 2020 US presidential election: evidence from survey experiments, 
# field experiments, and a campaign-level experiment. 
# Nature Human Behavior, forthcoming.

rm(list = ls())
library(tidyverse)
library(estimatr)
library(xtable)
# library(lmtest)
source('helper_file.R')

# load data and uncount
aggregated_set <- read_rds(file = "aggregated_analysis_set.rds")
main_analysis_set <- uncount(aggregated_set, weights = n)

all_covs <-
  c(
    'party_dem',
    'party_rep',
    'party_unknown',
    'party_other',
    'tss_30_40',
    'tss_40_50',
    'tss_50_60',
    'tss_60_70',
    'tss_100',
    'pts_100',
    "ideology_score_100",
    "partisan_score_100",
    "voted_in_2000",
    "voted_in_2002",
    "voted_in_2004",
    "voted_in_2006",
    "voted_in_2008",
    "voted_in_2010",
    "voted_in_2012",
    "voted_in_2014",
    "voted_in_2016",
    "voted_in_2018"
  )

cov_labels <-
  c(
    'Party: Democrat',
    'Party: Republican',
    'Party: Unknown',
    'Party: Other',
    'TSS: 30-40',
    'TSS: 40-50',
    'TSS: 50-60',
    'TSS: 60-70',
    'Trump support score / 100',
    'Turnout score / 100',
    "Ideology score / 100",
    "Partisanship score / 100",
    "Voted in 2000",
    "Voted in 2002",
    "Voted in 2004",
    "Voted in 2006",
    "Voted in 2008",
    "Voted in 2010",
    "Voted in 2012",
    "Voted in 2014",
    "Voted in 2016",
    "Voted in 2018"
  )


balance_regs <-
  lm_robust(formula = formula(paste0(
    "cbind(", paste0(all_covs, collapse = ", "), ") ~ condition"
  )),
  weights = ipw,
  data = main_analysis_set)

balance_df <-
  tidy(balance_regs) %>%
  filter(term == "conditiontreat") %>% 
  mutate(outcome = factor(outcome, levels = all_covs, labels = cov_labels),
         adjusted_p = p.adjust(p.value, method = "BH"))

g <- 
ggplot(balance_df, aes(estimate, outcome)) +
  geom_point() +
  geom_linerange(aes(xmin = conf.low, xmax = conf.high)) +
  geom_vline(xintercept = 0, color= "red", linetype = "dotted", alpha = 0.5) +
  scale_x_continuous(breaks = seq(-0.01, 0.01, 0.005)) +
  coord_cartesian(xlim = c(-0.01, 0.01)) +
  theme_minimal() +
  theme(axis.title.y = element_blank(),
        panel.grid.minor.x = element_blank()) +
  labs(x = "Differences in mean pre-treatment covariate values")

#ggsave("output/figure_5.pdf", width = 6.5, height = 3.5)



# Table_F3 --------------------------------------------------

table_f3 <-
  balance_df |>
  select(
    outcome,
    estimate,
    std.error,
    df,
    statistic,
    p.value,
    adjusted_p,
    conf.low,
    conf.high
  ) |> 
  arrange(outcome) |>
  xtable(digits = c(0, 0, 3, 3, 0, 3, 3, 3, 3, 3))

# print.xtable(
#   table_f3,
#   include.colnames = FALSE,
#   include.rownames = FALSE,
#   only.contents = TRUE,
#   hline.after = c(),
#   file = "output/table_F3.tex"
# )
